using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace WinFormsChartSamples
{
	/// <summary>
	/// Summary description for DrillDown.
	/// </summary>
	public class DrillDown : System.Windows.Forms.UserControl
	{
		private System.Windows.Forms.Label label9;
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.DataVisualization.Charting.Chart Chart1;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.Label label15;
		private System.Windows.Forms.Label label1;
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public DrillDown()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();
			
		}
		

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
            System.Windows.Forms.DataVisualization.Charting.Legend legend1 = new System.Windows.Forms.DataVisualization.Charting.Legend();
            System.Windows.Forms.DataVisualization.Charting.Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series();
            this.label9 = new System.Windows.Forms.Label();
            this.panel1 = new System.Windows.Forms.Panel();
            this.label6 = new System.Windows.Forms.Label();
            this.label5 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.label15 = new System.Windows.Forms.Label();
            this.Chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart();
            this.label1 = new System.Windows.Forms.Label();
            this.panel1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.Chart1)).BeginInit();
            this.SuspendLayout();
            // 
            // label9
            // 
            this.label9.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.label9.Font = new System.Drawing.Font("Verdana", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label9.Location = new System.Drawing.Point(16, 8);
            this.label9.Name = "label9";
            this.label9.Size = new System.Drawing.Size(702, 34);
            this.label9.TabIndex = 0;
            this.label9.Text = "This sample demonstrates how to drill down a chart. ";
            this.label9.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.label6);
            this.panel1.Controls.Add(this.label5);
            this.panel1.Controls.Add(this.label4);
            this.panel1.Controls.Add(this.label3);
            this.panel1.Controls.Add(this.label15);
            this.panel1.Location = new System.Drawing.Point(432, 56);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(292, 288);
            this.panel1.TabIndex = 19;
            // 
            // label6
            // 
            this.label6.Location = new System.Drawing.Point(64, 403);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(100, 23);
            this.label6.TabIndex = 5;
            this.label6.Text = "Border Size:";
            // 
            // label5
            // 
            this.label5.Location = new System.Drawing.Point(64, 380);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(100, 23);
            this.label5.TabIndex = 4;
            this.label5.Text = "Border Color:";
            // 
            // label4
            // 
            this.label4.Location = new System.Drawing.Point(64, 357);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(100, 23);
            this.label4.TabIndex = 3;
            this.label4.Text = "Hatch Style:";
            // 
            // label3
            // 
            this.label3.Location = new System.Drawing.Point(64, 334);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(100, 23);
            this.label3.TabIndex = 2;
            this.label3.Text = "Gradient:";
            // 
            // label15
            // 
            this.label15.Location = new System.Drawing.Point(64, 426);
            this.label15.Name = "label15";
            this.label15.Size = new System.Drawing.Size(100, 23);
            this.label15.TabIndex = 5;
            this.label15.Text = "Border Size:";
            // 
            // Chart1
            // 
            this.Chart1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(211)))), ((int)(((byte)(223)))), ((int)(((byte)(240)))));
            this.Chart1.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom;
            this.Chart1.BackSecondaryColor = System.Drawing.Color.White;
            this.Chart1.BorderlineColor = System.Drawing.Color.FromArgb(((int)(((byte)(26)))), ((int)(((byte)(59)))), ((int)(((byte)(105)))));
            this.Chart1.BorderlineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;
            this.Chart1.BorderlineWidth = 2;
            this.Chart1.BorderSkin.SkinStyle = System.Windows.Forms.DataVisualization.Charting.BorderSkinStyle.Emboss;
            chartArea1.Area3DStyle.Inclination = 15;
            chartArea1.Area3DStyle.IsClustered = true;
            chartArea1.Area3DStyle.IsRightAngleAxes = false;
            chartArea1.Area3DStyle.Perspective = 10;
            chartArea1.Area3DStyle.Rotation = 10;
            chartArea1.Area3DStyle.WallWidth = 0;
            chartArea1.AxisX.IsLabelAutoFit = false;
            chartArea1.AxisX.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            chartArea1.AxisX.LabelStyle.IsStaggered = true;
            chartArea1.AxisX.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea1.AxisX.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea1.AxisX2.Enabled = System.Windows.Forms.DataVisualization.Charting.AxisEnabled.False;
            chartArea1.AxisY.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            chartArea1.AxisY.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea1.AxisY.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea1.AxisY2.Enabled = System.Windows.Forms.DataVisualization.Charting.AxisEnabled.False;
            chartArea1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(165)))), ((int)(((byte)(191)))), ((int)(((byte)(228)))));
            chartArea1.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom;
            chartArea1.BackSecondaryColor = System.Drawing.Color.White;
            chartArea1.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea1.BorderDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;
            chartArea1.Name = "Default";
            chartArea1.Position.Auto = false;
            chartArea1.Position.Height = 80F;
            chartArea1.Position.Width = 80F;
            chartArea1.Position.X = 10F;
            chartArea1.Position.Y = 10F;
            chartArea1.ShadowColor = System.Drawing.Color.Transparent;
            this.Chart1.ChartAreas.Add(chartArea1);
            legend1.BackColor = System.Drawing.Color.Transparent;
            legend1.Enabled = false;
            legend1.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            legend1.IsTextAutoFit = false;
            legend1.Name = "Default";
            this.Chart1.Legends.Add(legend1);
            this.Chart1.Location = new System.Drawing.Point(16, 48);
            this.Chart1.Name = "Chart1";
            series1.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(180)))), ((int)(((byte)(26)))), ((int)(((byte)(59)))), ((int)(((byte)(105)))));
            series1.ChartArea = "Default";
            series1.CustomProperties = "LabelStyle=outside";
            series1.Legend = "Default";
            series1.Name = "Series1";
            this.Chart1.Series.Add(series1);
            this.Chart1.Size = new System.Drawing.Size(412, 296);
            this.Chart1.TabIndex = 0;
            this.Chart1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Chart1_MouseMove);
            this.Chart1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Chart1_MouseDown);
            this.Chart1.Click += new System.EventHandler(this.Chart1_Click);
            // 
            // label1
            // 
            this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.label1.Font = new System.Drawing.Font("Verdana", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label1.Location = new System.Drawing.Point(16, 360);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(702, 50);
            this.label1.TabIndex = 20;
            this.label1.Text = "Click on a column to drill down to a more detailed view that is represented by a " +
                "pie chart. Then, click on a pie slice to go back to the column chart.";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // DrillDown
            // 
            this.BackColor = System.Drawing.Color.White;
            this.Controls.Add(this.label1);
            this.Controls.Add(this.Chart1);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.label9);
            this.Font = new System.Drawing.Font("Verdana", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.Name = "DrillDown";
            this.Size = new System.Drawing.Size(728, 440);
            this.Load += new System.EventHandler(this.Form1_Load);
            this.panel1.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.Chart1)).EndInit();
            this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// Mouse Down Event
		/// </summary>
		private void Chart1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			// Call Hit Test Method
			HitTestResult result = Chart1.HitTest( e.X, e.Y );

			if( result.ChartElementType != ChartElementType.DataPoint )
				return;

			// Remove data points
			Chart1.Series[0].Points.Clear();

			// If Pie chart is selected
			if( Chart1.Series[0].ChartType == SeriesChartType.Pie )
			{
				// Set Chart Type
				Chart1.Series[0].ChartType = SeriesChartType.Column;

				// Add data points
				Chart1.Series[0].Points.Add( 5 );
				Chart1.Series[0].Points.Add( 6 );
				Chart1.Series[0].Points.Add( 7 );
				Chart1.Series[0].Points.Add( 2 );

				// Set Axis labels
				Chart1.Series[0].Points[0].AxisLabel = "N America";
				Chart1.Series[0].Points[1].AxisLabel = "S America";
				Chart1.Series[0].Points[2].AxisLabel = "Europe";
				Chart1.Series[0].Points[3].AxisLabel = "Asia";

				// Remove custom attributes
				Chart1.Series[0].CustomProperties = "";

				// Recalculate and repaint chart
				Chart1.ChartAreas[0].RecalculateAxesScale();
				Chart1.Invalidate();

				return;
			}

			// Set Label style for pie chart
			Chart1.Series[0].CustomProperties = "LabelStyle=outside";

			// Remove gradient for data points
			Chart1.Series[0].BackGradientStyle = GradientStyle.None;


			switch( result.PointIndex )
			{
				// N America
				case 0:
					// Add data points
					Chart1.Series[0].ChartType = SeriesChartType.Pie;
					Chart1.Series[0].Points.Add( 3 );
					Chart1.Series[0].Points.Add( 2 );
					Chart1.Series[0].Points.Add( 8 );

					// Set Axis labels
					Chart1.Series[0].Points[0].AxisLabel = "Country 1";
                    Chart1.Series[0].Points[1].AxisLabel = "Country 2";
                    Chart1.Series[0].Points[2].AxisLabel = "Country 3";
					
					break;
				// S America
				case 1:
					// Add data points
					Chart1.Series[0].ChartType = SeriesChartType.Pie;
					Chart1.Series[0].Points.Add( 4 );
					Chart1.Series[0].Points.Add( 6 );
					Chart1.Series[0].Points.Add( 2 );

					// Set Axis labels
                    Chart1.Series[0].Points[0].AxisLabel = "Country 4";
                    Chart1.Series[0].Points[1].AxisLabel = "Country 5";
                    Chart1.Series[0].Points[2].AxisLabel = "Country 6";
					
					break;
				// Europe
				case 2:
					// Add data points
					Chart1.Series[0].ChartType = SeriesChartType.Pie;
					Chart1.Series[0].Points.Add( 5 );
					Chart1.Series[0].Points.Add( 7 );
					Chart1.Series[0].Points.Add( 2 );
					Chart1.Series[0].Points.Add( 3 );

					// Set Axis labels
                    Chart1.Series[0].Points[0].AxisLabel = "Country 7";
                    Chart1.Series[0].Points[1].AxisLabel = "Country 8";
                    Chart1.Series[0].Points[2].AxisLabel = "Country 9";
                    Chart1.Series[0].Points[3].AxisLabel = "Country 10";
					
					break;
				// Asia
				case 3:
					// Add data points
					Chart1.Series[0].ChartType = SeriesChartType.Pie;
					Chart1.Series[0].Points.Add( 4 );
					Chart1.Series[0].Points.Add( 3 );
					Chart1.Series[0].Points.Add( 6 );
					Chart1.Series[0].Points.Add( 5 );
					Chart1.Series[0].Points.Add( 4 );

					// Set Axis labels
                    Chart1.Series[0].Points[0].AxisLabel = "Country 11";
                    Chart1.Series[0].Points[1].AxisLabel = "Country 12";
                    Chart1.Series[0].Points[2].AxisLabel = "Country 13";
                    Chart1.Series[0].Points[3].AxisLabel = "Country 14";
                    Chart1.Series[0].Points[4].AxisLabel = "Country 15";
					
					break;
			}
					
			Chart1.ChartAreas[0].RecalculateAxesScale();
			Chart1.Invalidate();
		}

		/// <summary>
		/// Mouse Move Event
		/// </summary>
		private void Chart1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			// Call Hit Test Method
			HitTestResult result = Chart1.HitTest( e.X, e.Y );

			// Reset Data Point Attributes
			foreach( DataPoint point in Chart1.Series[0].Points )
			{
				point.BackHatchStyle = ChartHatchStyle.None;
				point.BorderWidth = 1;
			}
			
			// If a Data Point or a Legend item is selected.
			if
			( 	result.ChartElementType == ChartElementType.DataPoint ||
				result.ChartElementType == ChartElementType.LegendItem )
			{				
				// Set cursor type 
				this.Cursor = Cursors.Hand;

				// Find selected data point
				DataPoint point = Chart1.Series[0].Points[result.PointIndex];

				// Set End Gradient Color to White
				point.BackSecondaryColor = Color.White;

				// Set selected hatch style
				point.BackHatchStyle = ChartHatchStyle.Percent25;

				// Increase border width
				point.BorderWidth = 2;
			}
			else
			{
				// Set default cursor
				this.Cursor = Cursors.Default;
			}
		
		}
		
		// Load Form
		private void Form1_Load(object sender, System.EventArgs e)
		{
			// Set Chart Type
			Chart1.Series[0].ChartType = SeriesChartType.Column;

			// Add Data Points
			Chart1.Series[0].Points.Add( 5 );
			Chart1.Series[0].Points.Add( 6 );
			Chart1.Series[0].Points.Add( 7 );
			Chart1.Series[0].Points.Add( 2 );

			// Add Axis label
			Chart1.Series[0].Points[0].AxisLabel = "N America";
			Chart1.Series[0].Points[1].AxisLabel = "S America";
			Chart1.Series[0].Points[2].AxisLabel = "Europe";
			Chart1.Series[0].Points[3].AxisLabel = "Asia";

			// Remove custom attributes
			Chart1.Series[0].CustomProperties = "";
		
		}

		private void Chart1_Click(object sender, System.EventArgs e)
		{
		
		}
	}
}
